package com.waimai.controllor.meituan;

import java.util.ArrayList;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;

import com.waimai.controllor.Interface.IHtmlControllor;
import com.waimai.log.Log;
import com.waimai.utils.HtmlParserUtils;
import com.waimai.utils.StringUtils;
import com.waimai.utils.htmlStrUtils;

public class MeituanHtmlControllor implements IHtmlControllor {

	private static MeituanHtmlControllor controllor;

	public static MeituanHtmlControllor getInstance() {
		if (controllor == null) {
			controllor = new MeituanHtmlControllor();
		}
		return controllor;
	}

	@Override
	public String getHrefUrl(Node n) {
		Node parentNode = n;
		String hrefUrl = "";
		Node hrefNode = HtmlParserUtils.getChildNode(parentNode,
				new HasAttributeFilter("class", "rest-atag"));
		String hrefText = hrefNode.getText();
		if (hrefText.contains("href")) {
			hrefUrl = htmlStrUtils.getMeituanHref(StringUtils.pattern(hrefText,
					"\".*\""));
		}
		return hrefUrl;
	}

	@Override
	public String getImgUrl(Node n) {
		// 图标，没分割
		Node parentNode = n;
		Node node = HtmlParserUtils.getChildNode(parentNode,
				new HasAttributeFilter("class", "scroll-loading"));
		String imgUrl = "";
		if (node != null) {
			imgUrl = htmlStrUtils.getMeituanImgUrl(node.getText());
		}
		return imgUrl;
	}

	@Override
	/**
	 * 营业 0 ; 休息 1
	 */
	public int getStatus(Node n) {
		// 休息
		Node parentNode = n;
		int status = 0;
		NodeFilter statusFilter = new HasAttributeFilter("class",
				"outof-sale borderradius-2");
		NodeList statusList = HtmlParserUtils.getChildrenNodeList(parentNode,
				statusFilter);
		if (statusList.size() != 0) {
			status = 1;
		}
		return status;
	}

	@Override
	public ArrayList<String> getDrink(Node n) {
		// 饮料
		Node parentNode = n;

		ArrayList<String> drink = new ArrayList<String>();

		try {
			NodeList drinkList = HtmlParserUtils.getChildrenNodeList(
					parentNode,
					new HasAttributeFilter("data-icon", "i-ph17x17"));
			for (int j = 0; j < drinkList.size(); j++) {
				Node drinkNode = drinkList.elementAt(j);
				String str = drinkNode.getText();
				if (str.contains("http://p0.meituan.net/xianfu/__43834064__9871548.png")) {
					drink.add("自选赠饮");
				} else if (str
						.contains("http://p1.meituan.net/xianfu/__43237563__3142624.png")) {
					drink.add("赠330ml可口可乐");
				} else if (str
						.contains("http://p0.meituan.net/xianfu/95d0fc09ec534ccff056f77a4d9a539f713.png")) {
					drink.add("赠500ml蜂蜜柚子");
				}
			}
		} catch (NullPointerException e) {
			Log.e(e, "getDrink error");
			e.printStackTrace();

		}
		return drink;
	}

	@Override
	public String getSendTime(Node n) {
		// 送餐时间
		Node parentNode = n;
		String sendTime = "";
		Node sendTimeNode = HtmlParserUtils.getChildNode(parentNode,
				new HasAttributeFilter("class", "send-time ct-middlegrey"));
		if (sendTimeNode == null) {
			sendTime = "";
		} else {
			sendTime = sendTimeNode.toPlainTextString();
		}
		return StringUtils.pattern(sendTime, "[0-9]分钟");
	}

	@Override
	public int getCompany() {
		return 0;
	}

	@Override
	public String getName(Node n) {
		// TODO Auto-generated method stub
		return getContentChild(n, "name");

	}

	@Override
	public int getMaxBack(Node n) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getTotalPerMon(Node n) {
		String totalPerMon = StringUtils.getIntStr(getContentChild(n,
				"total cc-oriange fl"));
		if ("".equals(totalPerMon)) {
			return "";
		} else {
			return totalPerMon + "份";
		}
	}

	@Override
	public String getStartPrice(Node n) {
		return StringUtils
				.pattern(getContentChild(n, "start-price"), "[0-9]*元");

	}

	@Override
	public String getSendPrice(Node n) {
		return StringUtils.pattern(getContentChild(n, "send-price"), "[0-9]*元");
	}

	private String getContentChild(Node n, String childClass) {
		Node contentNode = n;
		Node childNode = HtmlParserUtils.getChildNode(contentNode,
				new HasAttributeFilter("class", childClass));
		if (childNode == null) {
			return "";
		} else {
			return StringUtils.replaceBlank(childNode.toPlainTextString());
		}
	}

}
